package YAPclient;

import java.awt.Color;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import YAPenvironment.YAPconstant;
import YAPenvironment.YAPconstant.Command;


/**
 * This class is for creating dialogs which allow users to change their names.
 */

public class ChangeNameDialog extends JDialog implements MouseListener, ActionListener{
	
	
	private static final long serialVersionUID = -75677284502762751L;
	
	private LimitedTextField ltfEditName = new LimitedTextField(new SetMaxText(YAPconstant.C_MAXNAMELENGTH),"Enter new name");
	private javax.swing.JButton btnChangeName = new JButton("OK");
	private JLabel lblErrorMessage = new JLabel();
	
	
	/**
	 * Creates and opens a new ChangeNameDialog
	 * @param owner Frame which owns this dialog
	 */
	public ChangeNameDialog(JFrame owner){
		
		super(owner, "Change Name");
		
		ltfEditName.addMouseListener(this);
		ltfEditName.addMouseListener(this);
		btnChangeName.addActionListener(this);
		 
		this.addComponents();
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
	}
	
	
	private void addComponents(){
		
		this.getContentPane().setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.insets = new Insets(10,10,10,10);
		c.anchor = GridBagConstraints.WEST;
		
		c.gridx = 0; c.gridy = 0;
		c.gridwidth = 2; c.gridheight = 2;
		this.getContentPane().add(lblErrorMessage,c);
		
		c.gridwidth = 1; c.gridheight = 1;
		c.gridx = 0; c.gridy=2;
		this.getContentPane().add(new JLabel("New Name: "),c);
		
		c.gridx = 1; //c.gridy = 2;
		this.getContentPane().add(ltfEditName,c);
		
		c.gridx = 0; c.gridy=4;
		c.gridwidth=2; c.anchor = GridBagConstraints.CENTER;
		this.getContentPane().add(btnChangeName,c);
		
	}
	
	
	/**
	 * Adds an error message to the currently opened dialog
	 * @param errMsg Error message to be displayed
	 */
	public void setErrorMessage(String errMsg){
		this.lblErrorMessage.setText(errMsg);
		this.lblErrorMessage.setForeground(Color.RED);
		this.setSize(this.getPreferredSize());
		this.repaint();
	}
	
	
	/**
	 * Checks whether the user provided all necessary information, i.e. a new name
	 * @return 0 if everything is OK, else -1
	 */
	@SuppressWarnings("unused")
	private int checkUserInput(){
		if(ltfEditName.getText().equals("")){
			this.setErrorMessage("Please enter your name:");
			return -1;
		}
		return 0;
	}
	
	
	/**
	 * Handles user input from btnChangeName,
	 * i.e. sends the new name to the server (using the {@link ParseThread}'s writeMessage method)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnChangeName){
			//if(this.checkUserInput() == 0){
				DataModul.parseThread.writeMessage(Command.changeName.toString()+YAPconstant.C_SEPARATOR+ltfEditName.getText());
			//}
		}
	}

	/**
	 * Selects all content of the 'new name' input field as soon as the user clicks on it
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent evt) {
		if(evt.getSource()==ltfEditName)
			ltfEditName.selectAll();
	}
	
	@Override
	public void mouseReleased(MouseEvent evt) {}

	@Override
	public void mouseEntered(MouseEvent evt) {}

	@Override
	public void mouseExited(MouseEvent evt) {}

	@Override
	public void mousePressed(MouseEvent evt) {}


}
